/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jskat.net;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kai
 */
public class ClientSocket {
    
    private String hostAdress;
    private int port;
    private String data;
    private Socket clientSocket;
    private ObjectOutputStream out;
    private ObjectInputStream in;

    public ClientSocket(String aHostAdress, int aPort)
    {
        port = aPort;
        hostAdress= aHostAdress;
        try {
            clientSocket = new Socket(aHostAdress, aPort);
            out = new ObjectOutputStream(clientSocket.getOutputStream());
            in = new ObjectInputStream(clientSocket.getInputStream());
        } catch (UnknownHostException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    
    public String sendData(String aData) throws IOException
    {
        data = aData;
        
        out.writeObject(aData);
        out.flush();
        return data;
    }
    
    
    public String receiveData() throws IOException
    {
        try {
            data = (String)in.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("data received in unknown format");
        }
        return data;
    }
    
    public void endSession() throws IOException
    {
        in.close();
        out.close();
        clientSocket.close();

    }
    
    public boolean echoRespond()
    {
        String tmpMess = "";
        long tmpTime = System.currentTimeMillis();
        long tmpTime2;
        do {
            try {
                tmpMess = (String) in.readObject();
                if (tmpMess.equals("echo")) {
                    sendData("echo");
                    return true;
                }
                tmpTime2 = System.currentTimeMillis();
                if((tmpTime2 - tmpTime) >= 60000)
                {
                    return false;
                }
            } catch (IOException | ClassNotFoundException eX) {
                Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, eX);
            }
            
        } while (!(tmpMess.equals("echo")));
        
        return true;
    }
    
    public boolean echoHost()
    {
        try {
            sendData("echo");
        } catch (IOException ex) {
            Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        String tmpMess = "";
        long tmpTime = System.currentTimeMillis();
        long tmpTime2;
        do {
            try {
                tmpMess = (String) in.readObject();
                if (tmpMess.equals("echo")) {
                    return true;
                }
                tmpTime2 = System.currentTimeMillis();
                if((tmpTime2 - tmpTime) >= 60000)
                {
                    return false;
                }
            } catch (IOException | ClassNotFoundException eX) {
                Logger.getLogger(ClientSocket.class.getName()).log(Level.SEVERE, null, eX);
            }
            
        } while (!(tmpMess.equals("echo")));
        
        return true;
    }
    
    public String getHostAdress() {
        return hostAdress;
    }

    public void setHostAdress(String aHostAdress) {
        hostAdress = aHostAdress;
    }

    public String getData() {
        return data;
    }

    public void setData(String aData) {
        data = aData;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int aPort) {
        port = aPort;
    }

    public Socket getClientSocket() {
        return clientSocket;
    }

    public void setClientSocket(Socket aClientSocket) {
        clientSocket = aClientSocket;
    }

    public ObjectOutputStream getOut() {
        return out;
    }

    public void setOut(ObjectOutputStream anOut) {
        out = anOut;
    }

    public ObjectInputStream getIn() {
        return in;
    }

    public void setIn(ObjectInputStream anIn) {
        in = anIn;
    }
    
    
    
}
